package com.nerve.android.cloudoffice.app.activity.assist;

import java.util.ArrayList;
import java.util.List;

import org.nerve.android.annotation.Acvitity;
import org.nerve.android.ui.corner.CornerCell;
import org.nerve.android.ui.corner.CornerRowLayout;
import org.nerve.android.ui.corner.OnRowClickListener;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.view.View;

import com.nerve.android.cloudoffice.R;
import com.nerve.android.cloudoffice.app.NavigationActivity;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.domain.Department;
import com.nerve.android.cloudoffice.domain.User;
import com.nerve.android.cloudoffice.service.data.DepartmentData;

/**
 * @project: Cloudoffice
 * @file: UserSelectActivity.java
 * @package: com.nerve.android.cloudoffice.app.activity.assist
 * @description:
 *	user fot select a user
 * @author: 集成显卡	1053214511@qq.com
				IBM GDC, zengxm@cn.ibm.com
 * @date&time: Aug 16, 2013
 * @change log:
 */
@Acvitity(layout=R.layout.assist_select_department)
public class UserSelectActivity extends NavigationActivity
	implements OnRowClickListener{
	
	public final static String INFO = "info";
	public final static String ID = "selectId";
	
	private CornerRowLayout cornerL;
	private List<CornerCell> cells;
	
	private boolean userSelect;
	private int departmentId;
	private String selectInfo;
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			case WORKER_FAIL:
				toast(Const.ERROR);
				break;
			case WORKER_OK:
				cornerL.setCellList(cells);
				break;
			}
		}
	};
	
	@Override
	protected void initUI() {
		super.initUI();
		
		setNavTitle("选择职员");
		
		cornerL = (CornerRowLayout)findViewById(R.id.departmentList);
		cornerL.setShowValue(false);
		cornerL.setOnRowClickListener(this);
		
		new UserWorker().execute();
	}
	
	/**
	 * 当返回键被点击时
	 */
	@Override
	protected void onNavigateBackButtonClick() {
		if(userSelect){
			userSelect = false;
			new UserWorker().execute();
		}
		//只是在部门选择这一层时，直接退出
		else{
			setResult(RESULT_CANCELED, null);
			finish();
		}
	}

	@Override
	public void onRowClick(View v, int index) {
		CornerCell cell = cells.get(index);
		int id = -1;
		try{
			id = Integer.valueOf(cell.getValue());
		}catch(Exception e){
			Const.log(e);
			return;
		}
		
		if(id > 0)
			departmentId = id;
		
		if(userSelect){
			//当点击用户时，返回数据
			Intent intent = getIntent();
			selectInfo += Const.SPLIT + cell.getTitle();
			
			Const.log("UserSelectActivity 选择结果：" + selectInfo +" , id="+departmentId);
			
			intent.putExtra(INFO, selectInfo);
			intent.putExtra(ID, departmentId);
			setResult(RESULT_OK, intent);
			finish();
		}else{
			selectInfo = cell.getTitle();
			userSelect = true;
			new UserWorker().execute();
		}
	}
	
	private class UserWorker extends AsyncTask<Void, Void, Integer>{

		@Override
		protected Integer doInBackground(Void... params) {
			cells = null;
			cells = new ArrayList<CornerCell>();
			//首先是选择部门
			if(!userSelect){
				List<Department> list = DepartmentData.getDepartmentList();
				for(Department d:list){
					cells.add(new CornerCell(d.getName(), d.getId()+"", true));
				}
			}else{
				List<User> users = DepartmentData.getUserList(departmentId);
				for(User u:users)
					cells.add(new CornerCell(u.getName(), u.getId()+"", false));
			}
			return WORKER_OK;
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			handler.sendEmptyMessage(result);
		}
	}
}